package com.mall.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

public class CustomExceptionResolver implements HandlerExceptionResolver {

	@Override
	/**
	 * 全局的异常处理器
	 */
	public ModelAndView resolveException(HttpServletRequest arg0, 
			HttpServletResponse arg1, Object handle,Exception ex) {
		MyException myexception=null;
		Logger log=LogManager.getLogger("mall");
		String className = handle.toString();
		log.error(className+" : "+ex.getMessage());	
		//判断是否是自定义异常
			if(ex instanceof MyException){
				myexception=(MyException)ex;
			}else{
				myexception=new MyException("发生了未知错误,请与管理员联系");
			}
			String message = myexception.getMessage();
			ModelAndView mav=new ModelAndView();
						mav.addObject("message", message);
						mav.setViewName("error");
							return mav;
	}

}
